import { getCodeAPI, loginAPI } from '../../apis/login'
import { store } from '../../store/index';
Page({
  data: {
    countDownVisible: false,
    mobile: '',
    code: '',
  },
  async sendCode() {
    // 发送验证码
    const res = await getCodeAPI(this.data.mobile)
    this.setData({
      countDownVisible: true,
    })
    wx.showToast({
      title: '验证码' + res.data.code,
      duration: 3000
    })

  },
  countDownChange(ev) {
    this.setData({
      timeData: ev.detail,
      countDownVisible: ev.detail.minutes === 1 || ev.detail.seconds > 0,
    })
  },
  async login() {
    await loginAPI(this.data.mobile, this.data.code).then(res => {
      store.setUser(res.data.token, res.data.refreshToken)
      wx.switchTab({
        url: '/pages/index/index',
        success: (res) => {
          return res
        },
        fail: (res) => { return res },
      })

    }).catch(err => {
      wx.showToast({
        icon: 'none',
        title: err.message,
        duration: 2000,
      })
    })
  }

})
